﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace VerifyManage
{
    public partial class Form1 : Form
    {
        [DllImport("kernel32")]
        private static extern int WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        [DllImport(@".\TJAD_MD5.dll")]
        public static extern void MD5_Data_I(StringBuilder message, ref UInt32 A, ref UInt32 B, ref UInt32 C, ref UInt32 D, Int32 length);

        [DllImport(@".\TJAD_MD5.dll")]
        public static extern void MD5_Data_I(Byte[] message, ref UInt32 A, ref UInt32 B, ref UInt32 C, ref UInt32 D, Int32 length);

        [DllImport(@".\TJAD_AES.dll")]
        public static extern void AES_Encrypt_Data_I(Byte[] message, UInt32 A, UInt32 B, UInt32 C, UInt32 D, Int32 length);

        [DllImport(@".\TJAD_AES.dll")]
        public static extern void AES_Decrypt_Data_I(Byte[] message, UInt32 A, UInt32 B, UInt32 C, UInt32 D, Int32 length);

        public static Int32 TJAD_SIGN_WriteName(String file, String name)
        {
            UInt32 key1 = 0x32f86773;
            UInt32 key2 = 0x24e27931;
            UInt32 key3 = 0x81eefed5;
            UInt32 key4 = 0x1ff77e95;

            Byte[] bytebuff1 = new Byte[128];
            Byte[] bytebuff2 = new Byte[128];
            Byte[] bytebuff = new Byte[256];
            Byte[] temp;

            Random ro = new Random();

            for (int i = 0; i < 128; i++)
                bytebuff1[i] = Convert.ToByte((ro.Next()) % 256);

            UInt32 A = 0;
            UInt32 B = 0;
            UInt32 C = 0;
            UInt32 D = 0;

            MD5_Data_I(bytebuff1, ref A, ref B, ref C, ref D, 128);

            AES_Encrypt_Data_I(bytebuff1, key4, key2, key3, key1, 128);
            temp = System.Text.Encoding.Default.GetBytes(name);

            for (int i = 0; i < temp.Length; i++)
            {
                bytebuff2[i] = temp[i];
            }
            for (int i = temp.Length; i < 128; i++)
            {
                bytebuff2[i] = 0;
            }
            AES_Encrypt_Data_I(bytebuff2, A, B, C, D, 128);

            for (int i = 0; i < 128; i++)
            {
                bytebuff[i] = bytebuff1[i];
                bytebuff[i + 128] = bytebuff2[i];

            }

            FileStream ifile = File.Open(file, FileMode.Create);
            ifile.Write(bytebuff, 0, 256);
            ifile.Close();

            return 0;
        }

        public Form1()
        {
            InitializeComponent();
        }

        public static String FormatedMD5(UInt32 A, UInt32 B, UInt32 C, UInt32 D)
        {
            String value = "";
            Char[] array = new Char[32];

            for (int i = 0; i < 32; ++i)
            {
                array[i] = '0';
            }

            value = Convert.ToString(A, 16);
            value.CopyTo(0, array, 8 - value.Length, value.Length);

            value = Convert.ToString(B, 16);
            value.CopyTo(0, array, 16 - value.Length, value.Length);

            value = Convert.ToString(C, 16);
            value.CopyTo(0, array, 24 - value.Length, value.Length);

            value = Convert.ToString(D, 16);
            value.CopyTo(0, array, 32 - value.Length, value.Length);

            value = new String(array);

            return value;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (!System.IO.File.Exists(@".\TJAD_MD5.dll"))
            {
                MessageBox.Show("TJAD_MD5.dll文件不存在");
                this.Close();
            }
            if (!System.IO.File.Exists(@".\TJAD_AES.dll"))
            {
                MessageBox.Show("TJAD_AES.dll文件不存在");
                this.Close();
            }
            StringBuilder path = new StringBuilder(2048);
            FileInfo file_info = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
            GetPrivateProfileString("LocalFile", "HandWriting", "", path, 2048, file_info.Directory.FullName + @"\config.ini");
            tbOutputDir.Text = path.ToString();
        }

        private void SelectDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                tbOutputDir.Text = dialog.SelectedPath;
            }
        }

        private void SelectFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "jpg文件 (*.jpg)|*.jpg|dwg文件 (*.dwg)|*.dwg|所有文件|*.*";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                tbSignFile.Text = dialog.FileName;
            }
        }

        private void OK_Click(object sender, EventArgs e)
        {
            if (!Directory.Exists(tbOutputDir.Text))
            {
                MessageBox.Show("输出目录不存在");
                return;
            }
            DirectoryInfo dir = new DirectoryInfo(tbOutputDir.Text);

            if (!File.Exists(tbSignFile.Text))
            {
                MessageBox.Show("真实签名文件不存在");
                return;
            }

            if (tbSignerName.Text.Length == 0)
            {
                MessageBox.Show("请输入签名人姓名");
                return;
            }

            StringBuilder name = new StringBuilder(tbSignerName.Text);

            UInt32 A = 0;
            UInt32 B = 0;
            UInt32 C = 0;
            UInt32 D = 0;
            MD5_Data_I(name, ref A, ref  B, ref C, ref D, name.Length);

            char[] separater = {'.'};
            String ext = tbSignFile.Text.Split(separater).Last();

            String true_path = dir.FullName + @"\" + FormatedMD5(A, B, C, D) + @"." + ext;

            if (System.IO.File.Exists(true_path))
            {
                System.IO.FileInfo file_info = new System.IO.FileInfo(true_path);
                file_info.Delete();
            }
            System.IO.File.Copy(tbSignFile.Text, true_path);

            String path = dir.FullName + @"\" + name + @".vrf";
            int status = TJAD_SIGN_WriteName(path, name.ToString());
            if (status != 0)
            {
                MessageBox.Show("Vrf文件创建失败");
            }
            MessageBox.Show("创建成功");
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
